<template>
  <div class="app-container">
    <!--      <el-tabs v-model="activeName" @tab-click="handleClick">-->
    <div v-loading="listLoading">
      <asim-split-pane
        :min-percent="splitMinPrecent"
        :default-percent="defaultPercent"
        split="vertical"
        style="border: 1px solid #e8e8e8; min-height: 800px"
      >
        <!--              左侧工作流列表-->
        <template slot="paneL">
          <span v-if="!openSide">
            <section-title
              title=""
              open-class="el-icon-caret-right"
              @changePaneL="changePaneLWith"
            />
          </span>
          <span v-else>
            <section-title
              title="工作流列表"
              :can-fold="false"
              open-class="el-icon-caret-left"
              @changePaneL="changePaneLWith"
            />
            <work-flow-tree
              :data-source="dataSource"
              node-key="oid"
              :default-props="defaultProps"
              @change="clickNode"
            ></work-flow-tree>
          </span>
        </template>
        <!--              右侧内容页面-->
        <template slot="paneR">
          <!--工作流信息       图和基本信息展示       -->
          <span v-show="currentShow == 'workFlowInfo'">
            <optimize-work-flow-info
              :current-work-flow-node="currentWorkFlow"
            ></optimize-work-flow-info>
          </span>
          <!--计算结果              -->
          <section-title
            v-if="currentShow == 'calResult'"
            title="计算结果文件"
            :can-fold="false"
            open-class="el-icon-caret-left"
            @changePaneL="changePaneLWith"
          />
          <el-row v-if="currentShow == 'calResult'">
            <el-col :span="24" align="right" style="padding: 0 30px">
              <el-button
                icon="el-icon-data-analysis"
                type="primary"
                size="small"
                @click="drawOptimizationGraph"
              >
                Result优化趋势图
              </el-button>
            </el-col>
          </el-row>

          <div
            v-if="currentShow == 'calResult' && calResultData.length == 0"
            align="center"
          >
            暂无计算结果文件
          </div>
          <collapse-result
            v-if="currentShow == 'calResult'"
            :result-data="calResultData"
            @drawOne="drawOne"
          ></collapse-result>
        </template>
      </asim-split-pane>
    </div>

    <!-- 优化趋势图模态框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogVisible"
      width="56%"
      @close="handleDialogClose"
    >
      {{ optimizationData }}
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import asimSplitPane from '@/components/AsimSplitPane'
  import sectionTitle from '@/components/SectionTitle'
  import { getAction } from '@/api/action'
  import { BaseListMixin } from '@/mixins/BaseListMixin'
  import WorkFlowTree from '@/views/calculate-project/compoents/WorkFlowTree'
  import collapseResult from '@/views/calculate-project/compoents/CollapseResult'
  import OptimizeWorkFlowInfo from '@/views/calculate-project/compoents/OptimizeWorkFlowInfo'
  export default {
    name: 'StructuralOptimizationCalculation',
    components: {
      WorkFlowTree,
      asimSplitPane,
      sectionTitle,
      collapseResult,
      OptimizeWorkFlowInfo,
    },
    mixins: [BaseListMixin],
    data() {
      return {
        splitMinPrecent: 12,
        defaultPercent: 25,
        dataSource: [],
        openSide: true,
        filterText: '',
        currentCheckNodeData: {},
        // activeName: "first",
        listLoading: false,
        defaultProps: {
          children: 'children',
          label: 'wfName',
        },
        url: {
          list:
            '/presoftasim/calculateProject/loadWorkflowCalculation/treeList',
          detail: 'presoftasim/wf/workflow/detail',
          createCal:
            '/presoftasim/calculateProject/loadWorkflowCalculation/createCalProject',
          deleteCal:
            '/presoftasim/calculateProject/loadWorkflowCalculation/deleteCalProject',
          initiateCal:
            '/presoftasim/calculateProject/loadWorkflowCalculation/initiateCalProject',
          // activityList: '/presoftasim/calculate/activity/activityList',
          calResultList: '/presoftasim/calculate/result/category',
        },
        disableMixinCreated: true,
        wfList: [], //工作流列表
        currentCal: {}, //当前选中的计算项目
        currentWorkFlow: {}, //当前选中的工作流
        calParam: {
          //计算项目参数
          id: this.$store.state.user.id,
          userName: this.$store.state.user.name,
          workFlowID: '',
          description: '',
          category: '',
          name: '',
          oid: '',
        },
        calResultData: [],
        currentShow: 'workFlowInfo',
        optimizationData: {}, //结构优化趋势图数据
        titleMap: { optimizationGraph: '优化趋势图' }, //模态框标题
        dialogStatus: 'optimizationGraph', //当前标题
        dialogVisible: false, //是否打开模态框
      }
    },
    created() {
      this.getList()
    },
    methods: {
      // 模块title展示  最好调整到computed中
      changePaneLWith() {
        this.openSide = !this.openSide
        if (this.openSide) {
          this.splitMinPrecent = 12
          this.defaultPercent = 25
        } else {
          this.splitMinPrecent = (150 / window.innerWidth) * 30
          this.defaultPercent = (150 / window.innerWidth) * 30
        }
      },
      getList() {
        //获取工作流、计算项目数据
        this.listLoading = true
        this.currentShow = 'calResult'
        let that = this
        // 默认加载动作
        return getAction(this.url.list, {}).then((res) => {
          that.dataSource = res.data.records
          if (this.$route.query.target == null) {
            if (
              that.dataSource.length > 0 &&
              that.dataSource[0].children.length > 0 &&
              that.dataSource[0].children[0].children.length > 0
            ) {
              // 初始化默认动作  加载第一个三级菜单（若第一个三级节点存在）
              that.clickNode(that.dataSource[0].children[0].children[0])
            } else if (
              that.dataSource.length > 0 &&
              that.dataSource[0].children.length > 0
            ) {
              // 初始化默认动作  加载第一个二级菜单（若第一个三级节点不存在）
              that.clickNode(that.dataSource[0].children[0])
            } else {
              console.log('work flow list is empty!')
            }
          } else {
            this.getTarget()
          }
          that.listLoading = false
        })
      },

      //右侧tree组件中触发点击事件
      clickNode(data) {
        this.calItemData = data
        if (data.objectType === 'classify') {
          //二级菜单
          this.currentWorkFlow = data
          this.currentShow = 'workFlowInfo'
        } else if (data.objectType === 'guide') {
          //三级菜单
          this.currentCal = data
          this.calResultData = []
          this.getCalResult(data.oid)
        } else if (data.objectType === 'title') {
          //一级菜单
          this.titlePage = true
          this.currentCal = {}
          this.currentWorkFlow = {}
          this.isCalItemDetail = false
        }
      },
      getCalResult(oid) {
        this.currentShow = 'calResult'
        getAction(this.url.calResultList, { oid: oid }).then((res) => {
          if (res.data.length > 0) {
            this.calResultData = res.data.reverse()
            // 将数组中最后一个数字添加到数组第一个位置
            this.calResultData.unshift(this.calResultData.pop())
          }
        })
      },
      //跳转到优化趋势图界面
      drawOptimizationGraph: function () {
        this.$router.push({
          path: '/calculate/optimizationChart',
          query: { target: 'Result', currentCal: this.currentCal },
        })
      },
      //绘制当前文件优化趋势图
      drawOne: function (data) {
        this.$router.push({
          path: '/calculate/optimizationChart',
          query: { target: 'one', file: data, currentCal: this.currentCal },
        })
      },
      //  获取跳转地址参数
      getTarget: function () {
        if (this.$route.query.target != null) {
          let cal = Object.assign({}, this.$route.query.target)
          this.clickNode(cal)
        }
      },
    },
  }
</script>

<style scoped></style>
